package cn.reinforce.util.juhe.function;

import cn.reinforce.util.http.HttpClientUtil;
import cn.reinforce.util.juhe.common.SmsConsts;
import cn.reinforce.util.juhe.model.JuheResponse;
import cn.reinforce.util.juhe.model.Sms;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 幻幻Fate
 * @create 2019/12/2
 * @since
 */
public class SmsUtil {

    /**
     * 通用短信发送接口
     *
     * @param mobile     手机号
     * @param tplValue   模版
     * @param key        聚合key
     * @param templateId 模版id
     * @return
     */
    public static JuheResponse sendCommonSms(String mobile, String tplValue, String key, Integer templateId) {
        Map<String, Object> data = new HashMap<>();
        data.put("mobile", mobile);
        data.put("tpl_id", Integer.toString(templateId));
        data.put("tpl_value", tplValue);
        data.put("key", key);

        String result = HttpClientUtil.post(SmsConsts.SMS_URL, data, "", false).getResult();
        Gson gson = new GsonBuilder().create();
        JuheResponse response = gson.fromJson(result, JuheResponse.class);

        Sms sms = gson.fromJson(gson.toJson(response.getResult()), Sms.class);

        response.setSms(sms);

        return response;
    }

    /**
     * 验证码短信发送
     *
     * @param mobile     手机号
     * @param code       验证码
     * @param timeout    超时时间，分钟
     * @param templateId 短信模版id
     * @return 错误码
     */
    public static JuheResponse sendVerificationCodeSms(String mobile, String code, int timeout, String key, int templateId) {
        return sendCommonSms(mobile, "#code#=" + code + "&#minute#=" + timeout, key, templateId);
    }
}
